<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-item">
        <span class="span">数据抽取名称 </span>
        <el-input v-model="listQuery.extractionName" placeholder="请输入数据抽取名称" style="width: 200px" />
      </div>
      <!-- <div class="filter-item">
        <span>数据抽取类型:</span>
        <el-select v-model="listQuery.extractionType" clearable placeholder="请选择数据抽取类型" style="width: 200px">
          <el-option
            v-for="item in dictList('data_extraction_type')"
            :key="'type' + item.label"
            :label="item.label"
            :value="item.value" />
        </el-select>
      </div> -->
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-edit" @click="handleSingle">
        {{ $t('table.single') }}
      </el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-edit" @click="handleMultitaskings">
        {{ $t('table.multitasking') }}
      </el-button>
      <!-- <el-button v-waves v-permission="['dataextraction:DataExtraction:add']" class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        {{ $t('table.add') }}
      </el-button> -->
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ $t('table.search') }}
      </el-button>
      <el-button v-waves v-permission="['dataextraction:DataExtraction:delete']" class="filter-item" style="margin-left: 10px;" type="danger" icon="el-icon-delete" @click="handleBathDelete">
        {{ $t('table.delete') }}
      </el-button>
    </div>

    <el-table ref="table" :key="tableKey" v-loading="listLoading" element-loading-text="正在加载，请稍后" :data="list" border fit highlight-current-row style="width: 100%;" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" align="center" width="100" label="序号" />
      <el-table-column label="数据抽取名称" align="center" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.extractionName }}</span>
        </template>
      </el-table-column>

      <el-table-column label="是否启用" align="center" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.status | dictLabel('is_use') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="数据抽取类型" align="center" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.extractionType | dictLabel('extraction_type') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" fixed="right" align="center" min-width="180" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button v-permission="['dataextraction:DataExtraction:update']" type="text" size="small" icon="el-icon-edit" @click="handleUpdate(row)">
            {{ $t('table.edit') }}
          </el-button>
          <el-button v-permission="['dataextraction:DataExtraction:delete']" type="text" size="small" icon="el-icon-delete" class="delete-text-btn" @click="handleDelete(row)">
            {{ $t('table.delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <multitasking ref="multitasking" @refreshList="getList"></multitasking>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" :page-sizes="pageArray" @pagination="getList" />
    <single ref="single" @refreshList="getList" :propValue="dataToSend"></single>
    <data-extraction-form ref="form" @refreshList="getList" />

  </div>
</template>

<script>
import { deleteDataExtraction, fetchDataExtractionList, getDataExtraction } from '@/api/dataextractionrealtime/dataExtraction'
import waves from '@/directive/waves' // waves directive
import permission from '@/directive/permission/permission'
import dataExtractionForm from './dataExtractionForm'
import Pagination from '@/components/Pagination'
import multitasking from './child/multitasking.vue'
import single from './single/multitasking'
export default {
  name: 'DataExtractionList',
  components: { dataExtractionForm, Pagination, multitasking, single },
  directives: { waves, permission },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      multipleSelection: [],
      listLoading: true,
      pageArray: this.$store.getters.pageArray,
      listQuery: {
        extractionName: undefined,
        extractionType: undefined,
        page: 1,
        limit: this.$store.getters.defaultPageSize,
      },
      temp: {},
      dataToSend: 'create',
    }
  },
  computed: {
    ids() {
      const ids = []
      this.multipleSelection.forEach((ele) => {
        ids.push(ele.id)
      })
      return ids.join(',')
    },
  },
  created() {
    this.getList()
  },
  methods: {
    //单任务
    handleSingle() {
      this.dataToSend = 'create'
      this.$refs.single.open()
    },
    //多任务
    handleMultitaskings() {
      this.dataToSend = 'update'
      this.$refs.multitasking.open()
    },
    getList() {
      this.listLoading = true
      fetchDataExtractionList(this.listQuery).then((response) => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleCreate() {
      this.$refs.form.handleCreate()
    },
    handleUpdate(row) {
      console.log(row)
      if (row.extractionType == 0) {
        this.$refs.form.handleUpdate(row)
      } else {
        getDataExtraction(row.id).then((response) => {
          if (response.data.code === 200) {
            this.$refs.multitasking.update(response.data.data)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      }
    },
    handleDelete(row) {
      this.$confirm('删除任务会级联删除任务日志，是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteDataExtraction(row.id).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleBathDelete() {
      if (this.ids === '') {
        this.$message.warning('请选择至少一条数据')
        return
      }
      this.$confirm('确定删除所选数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteDataExtraction(this.ids).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep(::-webkit-scrollbar) {
  display: block !important;
}

.span {
  color: #606266;
  font-weight: 700;
  font-size: 14px;
}
</style>
